package com.demo.socketioclient;

import com.alibaba.fastjson.JSON;
import io.socket.client.IO;
import io.socket.client.Socket;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class SocketioClientApplication {

    public static Socket socket = null;

    public static List<String> videoList = new ArrayList<>();

    static {
        videoList.add("video_20220826_001");
        videoList.add("video_20220826_002");
        videoList.add("video_20220826_003");
        videoList.add("video_20220826_004");
        videoList.add("video_20220826_005");
        videoList.add("video_20220826_006");
    }

    // public static void main(String[] args) {
    //     System.out.println(MessageFormat.format("http://10.13.1.102:20000/hls/{0}/index.m3u8", "video_2020202020_001"));
    //
    // }
    public static void main(String[] args) {

        init();
    }

    public static void init() {
        // 服务端socket.io连接通信地址
        String url = "http://10.13.1.100:20000?terminalCode=20720D390F23";
        try {
            IO.Options options = new IO.Options();
            options.transports = new String[]{"websocket","xhr-polling","jsonp-polling"};
            //失败重试次数
            options.reconnectionAttempts = 10;
            //失败重连的时间间隔
            options.reconnectionDelay = 1000;
            //连接超时时间(ms)
            options.timeout = 500;
            socket = IO.socket(url, options);

            socket.on(Socket.EVENT_CONNECT, objects -> {
                socket.emit("sync-time", "{\"requestAt\":290531492022000}");
                socket.emit("subscribe", "videoList");
                socket.emit("subscribe", "showVideo");
                System.out.println("client: " + "连接成功");
            });
            socket.on(Socket.EVENT_DISCONNECT, objects -> {
                System.out.println("client: " + "断开");
            });
            socket.on("videoList", objects -> {
                System.out.println("[videoList]: " + JSON.toJSONString(objects));

                String jsonString = JSON.toJSONString(objects[0]).replaceAll("\\\\","");
                jsonString = jsonString.substring(1);
                jsonString = jsonString.substring(0, jsonString.length() - 1);

                System.out.println("收到视频列表拉取通知：" + jsonString);
                RtmpVideoListDTO dto = JSON.parseObject(jsonString, RtmpVideoListDTO.class);
                RtmpVideoCallBackDTO callBackDTO = new RtmpVideoCallBackDTO();
                // 返回的uid与收到的一致
                callBackDTO.setuId(dto.getUid());
                callBackDTO.setVideoList(videoList);
                System.out.println("订阅[videoListCallback],订阅内容：" + JSON.toJSONString(callBackDTO));
                socket.emit("videoListCallback", JSON.toJSONString(callBackDTO));
            });
            socket.on("showVideo", objects -> {
                System.out.println("[showVideo]: " + JSON.toJSONString(objects));
                System.out.println("开始推流..");
            });
            socket.connect();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

class RtmpVideoCallBackDTO {

    private String uId;

    private List<String> videoList;

    public String getuId() {
        return uId;
    }

    public void setuId(String uId) {
        this.uId = uId;
    }

    public List<String> getVideoList() {
        return videoList;
    }

    public void setVideoList(List<String> videoList) {
        this.videoList = videoList;
    }
}

class RtmpVideoListDTO {

    private String uid;
    private String terminalId;
    private String startTime;
    private String endTime;

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getTerminalId() {
        return terminalId;
    }

    public void setTerminalId(String terminalId) {
        this.terminalId = terminalId;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }
}
